<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script src="/js/plugins/form/jquery.form.js"></script>

		<script>
			$(function () {
				//回显
				var user =  JSON.parse(sessionStorage.getItem("user"));
				$(".info").renderValues(user);
				//背景图片需要手动回显
				$(".myBg").css("background-image","url('"+user.coverImgUrl+"')");

				//用js给form表单设置action的值
				$("#infoForm").attr('action','/users/'+user.id);

				//给保存绑定点击事件 异步提交表单
				$("#saveBtn").click(function () {
					$("#infoForm").ajaxSubmit(function (data) {
                        if (data.success) {
                            //将user重新设置到sessionStorge中
							sessionStorage.setItem("user",JSON.stringify(data.result));
							//跳转到首页
							window.location.href="/mine/profiles.html";
                        }
                    });
                });

				//设置一个值 用来判断是修改背景还是头像
				var flag=null;

				//给headImg 头像筐绑定点击事件
				$("#headImg").click(function () {
					//用代码的形式去触发点击
					$("#fileInput").click();
					flag=true;
                });

                //给背景绑定点击事件
                $("#coverBtn").click(function () {
                    //用代码的形式去触发点击
                    $("#fileInput").click();
                    flag=false;
                });

				//给上传图片的文本框绑定改变事件
                $("#fileInput").change(function () {
                    //有值才提交上传图片的表单
                  if ($(this).val()) {

                          //提交表单 上传图片
                          $("#fileForm").ajaxSubmit(function (data) {
                              console.log(data);
                              //上传成功
                              if (data.status == 1) {
                                  //修改头像
                                  if(flag){
                                  	  //图片回显给用户看效果
                                  	  $("#headImg").attr("src",data.url);
                                  	  //并且设置提交表单的头像隐藏域
                                  	  $("#infoForm input[name='headImgUrl']").val(data.url);
                                  }else{
                                      //修改背景
                                      $(".myBg").css("background-image","url('"+data.url+"')");
                                      //并且设置提交表单的背景隐藏域
                                      $("#infoForm input[name='coverImgUrl']").val(data.url);
                                  }
                              }
                          });
                    }
                });

            });
		</script>
	</head>

	<body>

		<form id="fileForm" method="post" action="/images" enctype="multipart/form-data">
			<!--隐藏的文件筐-->
			<input id="fileInput" type="file" name="file" style="display:none">
		</form>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post">
					<!--将提交方式改为put请求-->
					<input type="hidden" name="_method" value="put" >

					<!--用隐藏域来传递图片(头像和背景)信息-->
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
					<!--用隐藏域来传递邮箱 不修改 用来回显-->
					<input type="hidden" name="email" render-value="email">


				
					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName" >
					<label>性别</label><select class="form-control" name="gender" render-value="gender" >
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
